//
//  RunTimeScene.m
//  WidgetTest
//
//  Created by 李明明 on 15/10/26.
//  Copyright © 2015年 李明明. All rights reserved.
//

#import "RunTimeScene.h"
#import <objc/runtime.h>

#define Print(x)     NSLog(@"%@", x)

@interface RunTimeScene ()
@property (nonatomic, strong) IBOutlet UIButton *button;
@end

@implementation RunTimeScene

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    id result = object_getClass(self);
//    NSLog(@"%@", result);
    BOOL result;
//    NSString *sourceStr = @"test";
    // 判断是不是类
//    BOOL result = object_isClass(sourceStr);
    // 类的名字
//    const char *c = object_getClassName(sourceStr);
    
    // NSLog(@"%s", c);
    // 得到方法的名字
    const char *c = sel_getName(@selector(noti));
    // 通过名字得到方法
    NSSelectorFromString([NSString stringWithFormat:@"%s", c]);
    SEL sel = sel_getUid(c);
    
    
    result = [self respondsToSelector:sel];
    [self.button addTarget:self action:sel forControlEvents:UIControlEventTouchUpInside];
    IMP imp1 = class_getMethodImplementation([self class], @selector(test));
    // 两个方法互换
    Method method1 = class_getInstanceMethod([self class], @selector(test));
    Method method2 = class_getInstanceMethod([self class], @selector(noti));
    // 把noti的内容加到imp1里面
    class_addMethod([self class], @selector(noti), imp1, method_getTypeEncoding(method2));
    // 两个方法互换
    method_exchangeImplementations(method1, method2);
    [self.button addTarget:self action:@selector(test) forControlEvents:UIControlEventTouchUpInside];

    

    
    
}
- (void)test
{
    NSLog(@"test");
    self.view.backgroundColor = [UIColor grayColor];
}
- (void)noti
{
    NSLog(@"noti response");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
